﻿using System;
using System.Text;
using PFD;
using System.Drawing;
using System.Drawing.Imaging;

namespace RadarScreenExt
{

    public class RadarScreenExtension : IExtensionComponent, ISystemVarConnector<float>
    {
        private IExtensionFactory Factory;

        float fLastLatitude;
        float fLastLongitude;
        float fCurrentZoom;

        #region IExtensionComponent Members

        public void Initialize(IExtensionFactory factory)
        {
            Factory = factory;
            fCurrentZoom = 1.0f;
            fLastLatitude = fLastLongitude = 1.0f;

            Factory.RegisterSystemVariable_float("Longitude", this);
            Factory.RegisterSystemVariable_float("Latitude", this);
/*
            Texture t = Factory.GetGlobalPoolContainer().GetTexture("Radar");
            Bitmap bmp = new Bitmap(t.Width, t.Height);
            uint glType = gl.GL_UNSIGNED_SHORT_5_6_5;
            uint glFormat = gl.GL_RGB;

            if (t.IsTransparent)
            {
                glType = gl.GL_UNSIGNED_BYTE;
                glFormat = gl.GL_RGBA;
            }

            //  Draw image onto bitmap
            //  Extract image data into bmpdata
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Transparent);
                g.DrawString("RADAR", new Font("Courier", 14.0f, FontStyle.Bold), new SolidBrush(Color.Yellow), 20, 20);
            }
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, t.Width, t.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
            gl.BindTexture(gl.GL_TEXTURE_2D, t.Name);
            gl.TexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, t.Width, t.Height, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, bd.Scan0);
            bmp.UnlockBits(bd);
            bmp.Dispose();*/
        }

        public void GetComponentName(StringBuilder szName)
        {
            szName = new StringBuilder("RadarScreenExt");
        }

        #endregion

        #region ISystemVarConnector<float> Members

        public void DoUpdate(float newVal)
        {
            //  a new value for either longitude or latitude was received
            float fLon = Factory.GetSystemVariable_floatobject("Longitude").Value;
            float fLat = Factory.GetSystemVariable_floatobject("Latitude").Value;
/*
            Texture t = Factory.GetGlobalPoolContainer().GetTexture("Radar");
            Bitmap bmp = new Bitmap(t.Width, t.Height);
            uint glType = gl.GL_UNSIGNED_SHORT_5_6_5;
            uint glFormat = gl.GL_RGB;

            if (t.IsTransparent)
            {
                glType = gl.GL_UNSIGNED_BYTE;
                glFormat = gl.GL_RGBA;
            }
            //  decide whether or not to repaint the image
            //  if so, do repaint
            //  Draw image onto bitmap
            //  Extract image data into bmpdata
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.Clear(Color.Transparent);
                g.DrawString("RADAR", new Font("Courier", 14.0f, FontStyle.Bold), new SolidBrush(Color.Yellow), 20, 20);
            }
            BitmapData bd = bmp.LockBits(new Rectangle(0, 0, t.Width, t.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format16bppRgb565);
            gl.BindTexture(gl.GL_TEXTURE_2D, t.Name);
            gl.TexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, t.Width, t.Height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, bd.Scan0);
            bmp.UnlockBits(bd);*/
        }

        #endregion
    }

    static public class MyDll
    {
        static public RadarScreenExtension harry = new RadarScreenExtension();

    }
}
